package com.czy.util.json;


import com.czy.convert.Converter;
import com.czy.enums.IEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 序列化，将enum枚举转为json
 *
 * @author chenzy
 * @since 2019.12.19
 */

public class SerializerIEnum2T<I extends IEnum<T>,T> extends JsonSerializer<I> implements Converter<I, T> {
    @Override
    public void serialize(I value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeObject(convert(value));
    }

    @Override
    public T convert(I source) {
        if (source == null) {
            return null;
        }
        return source.getValue();
    }
}
